package org.dromara.hm.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.hm.domain.vo.HmIssueReceiptRecordsVo;
import org.dromara.hm.domain.bo.HmIssueReceiptRecordsBo;
import org.dromara.hm.service.IHmIssueReceiptRecordsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 出入库记录
 *
 * @author zy Hu
 * @date 2024-12-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hm/issueReceiptRecords")
public class HmIssueReceiptRecordsController extends BaseController {

    private final IHmIssueReceiptRecordsService hmIssueReceiptRecordsService;

    /**
     * 查询出入库记录列表
     */
    @SaCheckPermission("hm:issueReceiptRecords:list")
    @GetMapping("/list")
    public TableDataInfo<HmIssueReceiptRecordsVo> list(HmIssueReceiptRecordsBo bo, PageQuery pageQuery) {
        return hmIssueReceiptRecordsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出出入库记录列表
     */
    @SaCheckPermission("hm:issueReceiptRecords:export")
    @Log(title = "出入库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HmIssueReceiptRecordsBo bo, HttpServletResponse response) {
        List<HmIssueReceiptRecordsVo> list = hmIssueReceiptRecordsService.queryList(bo);
        ExcelUtil.exportExcel(list, "出入库记录", HmIssueReceiptRecordsVo.class, response);
    }

    /**
     * 获取出入库记录详细信息
     *
     * @param recordId 主键
     */
    @SaCheckPermission("hm:issueReceiptRecords:query")
    @GetMapping("/{recordId}")
    public R<HmIssueReceiptRecordsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long recordId) {
        return R.ok(hmIssueReceiptRecordsService.queryById(recordId));
    }

    /**
     * 新增出入库记录
     */
    @SaCheckPermission("hm:issueReceiptRecords:add")
    @Log(title = "出入库记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmIssueReceiptRecordsBo bo) {
        return toAjax(hmIssueReceiptRecordsService.insertByBo(bo));
    }

    /**
     * 修改出入库记录
     */
    @SaCheckPermission("hm:issueReceiptRecords:edit")
    @Log(title = "出入库记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmIssueReceiptRecordsBo bo) {
        return toAjax(hmIssueReceiptRecordsService.updateByBo(bo));
    }

    /**
     * 删除出入库记录
     *
     * @param recordIds 主键串
     */
    @SaCheckPermission("hm:issueReceiptRecords:remove")
    @Log(title = "出入库记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] recordIds) {
        return toAjax(hmIssueReceiptRecordsService.deleteWithValidByIds(List.of(recordIds), true));
    }
}
